﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using SocialIn.Biz;
using SocialIn.Core.Data;
using System.Data;
using System.Text;

namespace SocialIn.Web.Search
{
    public partial class VoluntaryActivitySearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitSetting();

                this.htTotalPage.Value = TotalItemCount(null, null, null).ToString();

                this.boardItemList.InnerHtml = ItemRead(1, null, null, null);
            }
        }

        private void InitSetting()
        {
            this.ddlLocation.DataValueField = "SubCategory";
            this.ddlLocation.DataTextField = "CodeName";

            this.ddlTalent.DataValueField = "SubCategory";
            this.ddlTalent.DataTextField = "CodeName";

            CommonData common = new CommonData();
            DataTable result = common.GetCodeMaster("M004").ReturnValue;
            DataRow row = result.NewRow();
            row["SubCategory"] = "-1";
            row["CodeName"] = "전체 지역";
            result.Rows.InsertAt(row, 0);

            ddlLocation.DataSource = result;
            ddlLocation.DataBind();

            result = common.GetCodeMaster("M003").ReturnValue;
            row = result.NewRow();
            row["SubCategory"] = "-1";
            row["CodeName"] = "전체 재능";
            result.Rows.InsertAt(row, 0);

            ddlTalent.DataSource = result;
            ddlTalent.DataBind();
        }

        [WebMethod]
        public static string ItemRead(int currentIndex, string location, string talent, string title)
        {
            int pageSize = 7;

            StringBuilder html = new StringBuilder(@"<table cellspacing='0' class='ResultTable'>");
            VoluntaryActivity vol = new VoluntaryActivity();
            Result<DataTable> resultVolunteer = vol.SearchVoluntaryActivity(pageSize, currentIndex, location, talent, title);            

            if (resultVolunteer.ReturnValue == null || resultVolunteer.ReturnValue.Rows.Count == 0)
            {
                html.Append("<tr><td colspan='3'>조회된 봉사 목록이 없습니다.</td></tr>");
            }
            else
            {
                foreach (DataRow item in resultVolunteer.ReturnValue.Rows)
                {
                    html.Append("<tr>");

                    html.AppendFormat("<td class='Cell'><img alt='[{0}]{1}' title='[{0}]{1}' src='{2}' /></td>"
                               , item["TalentName"], item["Desc"]
                               , VirtualPathUtility.ToAbsolute(item["ImagePath"].ToString()));

                    html.AppendFormat("<td class='Cell'>{0}에서 ", item["Place"]);
                    DateTime wishStart = Convert.ToDateTime(item["WishStartDate"]);
                    DateTime wishEnd = Convert.ToDateTime(item["WishEndDate"]);

                    html.AppendFormat("{0}에 <strong>{1}시간</strong>동안<br>"
                        , wishStart.ToString("yyyy년 MM월 dd일")
                        , (wishEnd - wishStart).Hours);

                    html.AppendFormat("<a href='VoluntaryActivityDetailBadmi.aspx?seq={1}' class='DetailLink'/>{0}</a></td>", item["Title"], item["Seq"]);
                    html.Append("</tr>");
                }
            }

            html.Append("</table>");

            return html.ToString();
        }

        [WebMethod]
        public static int TotalItemCount(string location, string talent, string title)
        {
            VoluntaryActivity vol = new VoluntaryActivity();
            return vol.GetVoluntaryActivityTotalCount(7, location, talent, title);
        }
    }
}
